package com.teufandsub.socialproximity.logic;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;

import com.teufandsub.socialproximity.services.HttpService;

/**
 * @author Bastien Mathias <bastienmathias@gmail.com>
 * @author Antoine Teuf <antoine.teuf@gmail.com>
 * @version 1.0
 * @since 2013-05-22
 * 
 * Class to store informations to send for an Authentication.
 */

public class AuthenticationInfo {

	public final static String USERNAME_NAME = "username";
	public final static String PASSWD_NAME   = "passwd";
	public final static String REGID_NAME    = "regid";
	
	private String password, username, regid;

	/**
	 * Constructor of AuthenticationInfo
	 * 
	 * @param password String password used to connect
	 * @param username String username used to connect.
	 * @param regid String regId of the device which send the authentication.
	 */
	public AuthenticationInfo(String password, String username, String regid) {
		super();
		this.password = password;
		this.username = username;
		this.regid = regid;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}
	
	public String getRegid() {
		return regid;
	}

	public void setRegid(String regid) {
		this.regid = regid;
	}

	@Override
	public String toString() {
		return "AuthenticationInfo [password=" + password + ", username="
				+ username + ", regid=" + regid + "]";
	}

	/**
	 * Method used to convert AuthenticationInformation to a JSONObject.
	 * 
	 * @return {@link JSONObject} return the JSONObject implementation of the AuthenticationInfo
	 * @throws JSONException
	 */
	private JSONObject toJSONObject() throws JSONException {
		JSONObject jSonObject = new JSONObject();
		jSonObject.put(USERNAME_NAME, this.username);
		jSonObject.put(PASSWD_NAME, this.password);
		jSonObject.put(REGID_NAME, this.regid);
		
		return jSonObject;
	}
	
	/**
	 * Method used to send Authentication Information to the cloud application.
	 * @param context Context of the Application.
	 * @return JSONObject return value of the authentication asking.
	 */
	public JSONObject send(Context context) {
		HttpService httpService = new HttpService(context);
		JSONObject ret=null;
		try {
			ret = httpService.authentication(this.toJSONObject());
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return ret;
	}
}
